package com.gxa.learning.pc.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.common.JsonResult;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Course;
import com.gxa.learning.core.pojo.CourseForMain;
import com.gxa.learning.core.pojo.ProjectApply;
import com.gxa.learning.pc.service.CourseService;
import com.gxa.learning.pc.service.ProjectApplyService;
import com.gxa.learning.pc.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author lijianmin
 * @date 2021/12/07  12:24:05
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户的接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    ProjectApplyService projectApplyService;

    @Autowired
    CourseService courseService;

    @GetMapping("select")
    @ApiOperation("创建审批流程时添加流程节点时调用 获取所有管理员")
    @ApiImplicitParam(name = "name", value = "模糊查询字段", paramType = "query")
    public Map<String, Object> select(String name) {
        List<Map<String, Object>> list = userService.selectByMark(name);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(list);
        jsonResult.setMsg("查询成功!");

        return jsonResult.getDataMap();

    }

    @GetMapping("/projectList")
    @ApiOperation("查询对应用户所有培训项目列表")
    public Map<String, Object> projectList(Integer page, Integer size, String userId, String projectName, String type, String projectStatus) throws Exception {
        PageInfo<Map<String, Object>> userProjectList = userService.getUserProjectList(page, size, userId, projectName, type, projectStatus);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功!");
        jsonResult.put("total", userProjectList.getTotal());
        jsonResult.put("list", userProjectList.getList());
        return jsonResult.getDataMap();
    }

    @GetMapping("/courseList")
    @ApiOperation("查询对应用户所有课程列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目编号,没有项目id则查询该用户全部,课程"),
            @ApiImplicitParam(name = "userId", value = "用户编号", required = true)
    })
    public Map<String, Object> courseList(String projectId, String userId) throws Exception {
        List<Map<String, Object>> courseList = userService.getCourseList(projectId, userId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(courseList);
        jsonResult.put("total", courseList.size());
        jsonResult.setMsg("查询成功!");
        return jsonResult.getDataMap();
    }


    @PostMapping("/getRecommend")
    @ApiOperation("获取推荐培训")
    public Map<String, Object> getRecommend() {
        JsonResult jsonResult = new JsonResult();
        List<Map> recommend = userService.getRecommend();
        jsonResult.setCode("1");
        jsonResult.setData(recommend);
        return jsonResult.getDataMap();
    }

    @GetMapping("/getSigRecommend")
    @ApiOperation("获取单个培训的详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "培训项目的id", dataType = "long", required = true)
    })
    public Map getSigRecommend(Long id) {
        JsonResult jsonResult = new JsonResult();
        Map count = userService.getCount(id);
        System.out.println(count);
        Map map = userService.selectDetailRecommend(id);
        System.out.println(map);
        jsonResult.setCode("1");
        jsonResult.put("counts", count);
        jsonResult.put("detail", map);
        return jsonResult.getDataMap();
    }

    @PostMapping("/signUp")
    @ApiOperation("用户报名的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "project", value = "project对象")
    })
    public Map SignUp(@RequestBody ProjectApply projectApply) throws BusinessException {

        projectApply.setPay(true);
        projectApply.setApplyStatus(0);
        //要先判断是不是为空，是空才报名,因为报名有三种状态，所以三种状态都要判断
        List<ProjectApply> select = projectApplyService.select(projectApply);

        projectApply.setApplyStatus(1);
        List<ProjectApply> select1 = projectApplyService.select(projectApply);

        projectApply.setApplyStatus(2);
        List<ProjectApply> select2 = projectApplyService.select(projectApply);
        if (select.size() == 0 && select1.size() == 0 && select2.size() == 0) {
            //如果数据库没有报名信息，就把报名信息添加到数据库并把报名状态设置为0
            projectApply.setApplyStatus(0);
            ProjectApply insert = projectApplyService.insert(projectApply);
        } else {
            throw new BusinessException(ExceptionCode.ERORR_SIGUP);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功");

        return jsonResult.getDataMap();
    }

    @GetMapping("/getRecommendCourseDetail")
    @ApiOperation("获取推荐课程的详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "课程(course)的id", dataType = "long", required = true)
    })
    public Map getRecommendCourseDetail(Long id) {
        CourseForMain recommendCourseDetail = userService.getRecommendCourseDetail(id);

        Map courseCount = userService.getCourseCount(recommendCourseDetail.getId());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("detail", recommendCourseDetail);
        jsonResult.put("score", courseCount);
        return jsonResult.getDataMap();
    }

    @GetMapping("/learnPV")
    @ApiOperation("我要学习的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "课程(course)的id", dataType = "long", required = true)
    })
    public Map learnPV(Long id) {
        Course course = courseService.selectById(id);

        Map courseCount = userService.getCourseCount(id);
        String accessory = course.getAccessory();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("address", accessory);
        jsonResult.put("score", courseCount);
        return jsonResult.getDataMap();
    }

    @GetMapping("/getCourseInRecommend")
    @ApiOperation("获取培训项目下的所有课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "培训的id", dataType = "long", required = true)
    })
    public Map getCourseInRecommend(Long id) {
        JsonResult jsonResult = new JsonResult();
        List<Map> courseInRecommend = userService.getCourseInRecommend(id);
        jsonResult.setCode("1");
        jsonResult.put("total", courseInRecommend.size());
        jsonResult.setData(courseInRecommend);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getMyCourse")
    @ApiOperation("搜索课程")
    public Map getMyCourse(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        PageInfo<Map> myCourse = userService.getMyCourse(map);
        jsonResult.setCode("1");
        jsonResult.setData(myCourse.getList());
        jsonResult.put("count", myCourse.getTotal());
        return jsonResult.getDataMap();
    }
}
